<?php
class Article_Action extends BaseAction
{


    public function search()
    {
        $uri = $this->getSegmentArray();
        $cp = isset($uri[4]) ? $uri[4] : 0;
        $this->getController()->load->library('pagination');
        $this->getController()->pagination->initialize(array("uri_segment" => 4, "total_rows" => $this->getModel()->getManageTotalCount(), "per_page" => 10, "base_url" => "/index.php/admin/article/search/", "cur_page" => $cp));
        $data = array("articles" => $this->getModel()->managepagination(10, $cp), "pagination" => $this->getController()->pagination, "template" => "article_search.php");
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    /**
     *添加文章
     */
    public function add()
    {
        $this->getController()->load->library('session');
        $this->getController()->session->set_userdata("tempUploadList", array());
        $this->getController()->load->model("Article_Type_Model");
        $data = array("template" => "article_add.php", "menu" => "articleType", "parentType" => $this->getController()->Article_Type_Model->listParents());
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    public function batchRemove()
    {
        if (isset($_GET['ids'])) {
            $ids = explode(",", $_GET['ids']);
            $this->getModel()->batchRemove($ids);
            die("success");
        }
        die("failure");


    }

    public function edit()
    {
        $this->getController()->load->library('session');
        $this->getController()->session->set_userdata("tempUploadList", array());
        $this->getModel()->setId($_GET['id']);
        $domain = $this->getModel()->load();
        $data = array("template" => "article_add.php", "domain" => $domain, "menu" => "articleType");
        $this->getController()->load->model("Article_Type_Model");
        $data['parentType'] = $this->getController()->Article_Type_Model->listParents();
        $this->getController()->Article_Type_Model->id = $domain->type_id;
        $data['typeName'] = $this->getController()->Article_Type_Model->load()->name;
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    public function saveAdd()
    {
        $message = "";

        if (isset($_POST['id'])) {
            $message = "更新成功";
            $_POST['update_time'] = date("Y-m-d H:i:s");
            $this->getModel()->update($_POST);
            $this->addAttachment($_POST['id']);
        } else {
            $message = "添加成功";
            $_POST['update_time'] = date("Y-m-d H:i:s");
            $_POST['create_time'] = date("Y-m-d H:i:s");
            $this->getModel()->insert($_POST);
            $this->addAttachment($this->getModel()->getInsertId("id"));

        }

        $this->getController()->load->model("Article_Type_Model");
        $data = array("template" => "article_add.php", "message" => $message, "menu" => "articleType", "parentType" => $this->getController()->Article_Type_Model->listParents());
        $this->getController()->load->vars($data);
        $this->getController()->load->view("admin");
    }

    public function addAttachment($operator_id)
    {
        $this->getController()->load->library('session');
        foreach ($this->getController()->session->userdata("tempUploadList") as $key => $value) {
            $value['operator_id'] = $operator_id;
            $this->getController()->load->model("Attachment_Model");
            $this->getController()->Attachment_Model->insert($value);
        }
        $this->getController()->session->set_userdata("tempUploadList", array());
    }


}